package edu.hawaii.myisern.action;

import java.text.DateFormat;
import java.text.Format;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;

import edu.hawaii.myisern.model.MyIsernModel;
import edu.hawaii.myisern.sanitycheck.SanityCheckErrorItem;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * Provides a web interface for the MyIsern project.
 * 
 * @author Sonwright M. Gomez
 *
 */
public class MyIsernActionBean implements ActionBean {
  /** Required by the ActionBean interface. */
  private ActionBeanContext context;
  /** Our single MyIsern instance manipulated by all webapp users. */
  private MyIsernModel myIsernModel = MyIsernModel.getInstance();
  /** The username for all MyIsern Members. */
  

  /** An error string, always displayed, but invisible if empty. */
  private String errorMessage = "";
  private boolean checkResearchers = true;
  private boolean checkOrganizations = true;
  private boolean checkCollaborations = true;
  private String filter = "all";
    
  /**
   * Returns the context. Required by the interface. 
   * @return the ActionBeanContext.
   */
  public ActionBeanContext getContext() {
    return context;
  }
  
  /**
   * Sets the context. Required by the interface. 
   * @param context The ActionBeanContext to set. 
   */
  public void setContext(ActionBeanContext context) {
    this.context = context;
  }
  
  /**
   * A handler that performs the action when the 'push' button is pressed.
   * Performs a sanity check.
   * @return A Resolution indicating to redisplay the index.jsp page.
   *
   */
  public Resolution checkSanity() {
    return new ForwardResolution("/sanityCheckReprort.jsp");
  }

  /**
   * Gets the system time.
   * 
   * @return the current time.
   */
  public String getTime() {
    Calendar now = Calendar.getInstance();
    Format fullUSDate = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
    // SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd
    // HH:mm:ss");

    return fullUSDate.format(now.getTime());
  }
  /**
   * Researcher filter flag.
   * @return True/False
   */
  public boolean getCheckResearchers() {
    return this.checkResearchers;
  }
  /**
   * Organization filter flag.
   * @return True/False
   */
  public boolean getCheckOrganizations() {
    return this.checkOrganizations;
  }
  /**
   * Collaboration filter flag.
   * @return True/False
   */
  public boolean getCheckCollaborations() {
    return this.checkCollaborations;
  }
  /**
   * Displays the Sanity Check Report Page.
   * @return ForwardResolution to redisplay the scReport.jsp page
   */
  public Resolution sanityCheckReport() {
    return new ForwardResolution("/scReport.jsp");
  }
  /**
   * Updates the Sanity Check Report page with filter applied.
   * @return ForwardResolution to redisplay the scReport.jsp page
   */
  public Resolution filterReport() {
    return new ForwardResolution("/scReport.jsp");
  }
  /**
   * Returns Filter string.
   * @return filter string.
   */
  public String getFilterSetting() {
    return this.filter;
  }
  /**
   * Applies filtering for report.
   * @param filter Filter setting.
   */
  public void setFilterSetting(String filter) {
    this.filter = filter;
    if ("researchers".equals(filter)) {
      this.checkCollaborations = false;
      this.checkOrganizations = false;
      this.checkResearchers = true;
    }
    else if ("organizations".equals(filter)) {
      this.checkCollaborations = false;
      this.checkResearchers = false;
      this.checkOrganizations = true;
    }
    else if ("collaborations".equals(filter)) {
      this.checkOrganizations = false;
      this.checkResearchers = false;
      this.checkCollaborations = true;
    }
    else if ("all".equals(filter)) {
      this.checkCollaborations = true;
      this.checkOrganizations = true;
      this.checkResearchers = true;
    }
  }
  /**
   * Gets the list of errors.
   * 
   * @return The report iterator.
   * 
   */
  public Iterator<SanityCheckErrorItem> getReportIterator() {
    try {
      return myIsernModel.getReportIterator();
    }
    catch (Exception e) {
      return null;
    }
  }
  

}
